/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 强仔
 * Date: 2021-10-13
 * Time: 1:28
 */
public class TestDemo {
//搜索插入位置
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length-1;
        int mid = 0;
        while(left <= right){
            mid = (left+right)/2;
            if(nums[mid] > target){
                right = mid-1;
            }else if(nums[mid] < target){
                left = mid+1;
            }else{
                return mid;
            }
        }
        return mid;
    }
//删除重复数据
    public int removeDuplicates(int[] nums) {
        if(nums.length == 0){
            return 0;
        }
        int len = 1;
        int j = 1;
        int i = 0;
        while (j < nums.length) {
            if (nums[i] == nums[j]) {
                j++;
            } else {
                nums[i + 1] = nums[j];
                i++;
                len++;
            }
        }
        return len;
    }
//删除指定元素
    public int removeElement(int[] nums, int val) {
        int len = 0;
        for(int i = 0; i < nums.length;i++){
            if (nums[i] == val) {
                for(int j = i+1; j < nums.length;j++){
                    if (nums[j] == val) {
                        continue;
                    } else {
                        nums[i] = nums[j];
                        i++;
                        len++;
                    }
                }
                break;
            }
            len++;
        }
        return len;
    }
}
